package com.samoiloff.ui.list.smart {
	import com.samoiloff.framework.AbstractComponent;
	import com.samoiloff.ui.list.smart.controller.SmartListController;
	import com.samoiloff.ui.list.smart.interfaces.CellRenderer;
	import com.samoiloff.ui.list.smart.model.SmartListLayout;
	import com.samoiloff.ui.list.smart.model.SmartListModel;
	import com.samoiloff.ui.list.smart.view.SmartListRenderers;
	import com.samoiloff.ui.list.smart.view.SmartListView;

	import flash.events.Event;

	/**
	 * @author Samoiloff
	 */
	public class SmartList extends AbstractComponent {
		
		private var _lModel	: SmartListModel;
		private var _lView	: SmartListView;

		public function SmartList() {
			super();

			_lModel = _model as SmartListModel;
			_lView = _lModel.view;
			
			addListeners();
		}
		
		public function render() : void {
			_lModel.render();
		}
		
		override protected function getModelClass() : Class {
			return SmartListModel;
		}
		
		override protected function getViewCls() : Class {
			return SmartListView;
		}
		
		override protected function getControllerCls() : Class {
			return SmartListController;
		}

		override public function destroy(): void {
			removeListeners();
			
			_lModel = null;
			_lView = null;
			
			super.destroy();
		}

		private function onSelectedChanged(e:Event): void {
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function addListeners():void {
			_model.addEventListener(Event.RESIZE, dispatchEvent);
			_model.addEventListener(SmartListModel.SELECTED_INDEX_CHANGED, onSelectedChanged);
		}
		
		private function removeListeners():void {
			_model.removeEventListener(Event.RESIZE, dispatchEvent);
			_model.removeEventListener(SmartListModel.SELECTED_INDEX_CHANGED, onSelectedChanged);
		}

//		private function _lModel: SmartListModel {
//			return _model as SmartListModel;
//		}
//		
//		private function getView() : SmartListView {
//			return _view as SmartListView;
//		}
		
		public function get renderers() : SmartListRenderers {
			return _lView.renderers;
		}
		
		/*
		 * cellData
		 */
		public function set cellRenderer(cellRenderer:CellRenderer):void {
			_lModel.cellRenderer = cellRenderer;
		}
		
		public function get cellRenderer():CellRenderer {
			return _lModel.cellRenderer;
		}
		
		/*
		 * dataProvider
		 */
		public function set dataProvider(dataProvider:Array):void {
			_lModel.dataProvider = dataProvider;
		}
		
		public function get dataProvider():Array {
			return _lModel.dataProvider;
		}
		/*
		 * width
		 */
		override public function get width():Number {
			return _lModel.width;
		}
		
		override public function set width(value: Number): void {
			_lModel.width = value;
		}
		/*
		 * height
		 */
		override public function get height(): Number {
			return _lModel.height;
		}

		override public function set height(value: Number): void {
			_lModel.height = value;
		}
		/*
		 * layout
		 */
		public function get layout(): SmartListLayout {
			return _lModel.layout;
		}

		public function set layout(value: SmartListLayout): void {
			_lModel.layout = value;
		}
		
		/*
		 * selected
		 */
		public function get selected():* {
			return _lModel.dataProvider[_lModel.selectedIndex];
		}
		
		public function set selected(data:*):void {
			removeListeners();
			_lModel.selectedIndex = _lModel.dataProvider.indexOf(data);
			addListeners();
		}
		/*
		 * selecterRenderer
		 */
		public function get selectedRenderer():CellRenderer {
			return _lView.viewport.getItemAt(_lModel.selectedIndex);
		}
		
		public function set selectedRendere(selectedRenderer:CellRenderer):void {
			_lModel.selectedIndex = selectedRenderer.index;
		}
		/*
		 * selectedIndex
		 */
		public function get selectedIndex():int {
			return _lModel.selectedIndex;
		}
		
		public function set selectedIndex(selectedIndex:int):void {
			removeListeners();
			_lModel.selectedIndex = selectedIndex;
			addListeners();
		}
		
		/*
		 * scrollPosition
		 */
		
		public function get scrollPosition():int {
			return _lView.scroll.scrollPosition;
		}
		
		public function set scrollPosition(scrollPosition:int):void {
			_lView.scroll.scrollPosition = scrollPosition; 
		}
		
	}
}
